using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

[CustomEditor (typeof (PathMapBehaviour))]
public class PathMapInspector : Editor 
{
	void OnEnable ()
	{
		m_pathMapController = target as PathMapBehaviour;
		
		m_jointsToCleared = new List<PathJointBehaviour> ();
		m_pathsToCleared = new List<PathPairBehaviour> ();
	}
	
	public override void OnInspectorGUI ()
	{
		m_jointsToCleared.Clear ();
		m_pathsToCleared.Clear ();
		
		Color oldColor = GUI.color;
		GUI.color = Color.green;
		GUILayout.Button ("Build Path Map");
		GUI.color = oldColor;
		
		// list all path joints
		EditorTools.drawSeperator ();
		EditorGUILayout.LabelField ("Path Joints");
		foreach (PathJointBehaviour pathJointController in m_pathMapController.m_pathJointControllers) 
		{
			DrawPathJoint (pathJointController);
		}
		
		// list all paths
		EditorTools.drawSeperator ();
		EditorGUILayout.LabelField ("Paths");
		foreach (PathPairBehaviour pathController in m_pathMapController.m_pathControllers) 
		{
			DrawPathPair (pathController);
		}
		
		foreach (PathJointBehaviour pathJointController in m_jointsToCleared)
		{
			m_pathMapController.RemovePathJointController (pathJointController);
			DestroyImmediate (pathJointController.gameObject);
		}
		
		foreach (PathPairBehaviour pathController in m_pathsToCleared)
		{
			m_pathMapController.RemovePathController (pathController);
			DestroyImmediate (pathController.gameObject);
		}
	}
	
	void OnSceneGUI ()
	{
		
	}
	
	void DrawPathJoint (PathJointBehaviour pathJointController)
	{
		EditorGUILayout.BeginHorizontal ();
		EditorGUILayout.ObjectField (pathJointController.gameObject, typeof (GameObject));
		EditorGUILayout.Space ();
		Color oldColor = GUI.color;
		
		if (GUILayout.Button ("Show"))
		{
			m_pathMapController.Draw (pathJointController);
		}
		
		GUI.color = Color.red;
		if (GUILayout.Button ("Delete"))
		{
			/*
			m_pathMapController.RemovePathJointController (pathJointController);
			DestroyImmediate (pathJointController.gameObject);
			*/
			
			m_jointsToCleared.Add (pathJointController);
		}
		GUI.color = oldColor;
		EditorGUILayout.EndHorizontal ();
	}
	
	void DrawPathPair (PathPairBehaviour pathBehaviour)
	{
		EditorGUILayout.BeginHorizontal ();
		EditorGUILayout.ObjectField (pathBehaviour.gameObject, typeof (GameObject));
		EditorGUILayout.Space ();
		Color oldColor = GUI.color;
		
		if (GUILayout.Button ("Show"))
		{
			m_pathMapController.Draw (pathBehaviour);
		}
		
		GUI.color = Color.red;
		if (GUILayout.Button ("Delete"))
		{
			/*
			m_pathMapController.RemovePathController (pathBehaviour);
			DestroyImmediate (pathBehaviour.gameObject);
			*/
			m_pathsToCleared.Add (pathBehaviour);
		}
		
		GUI.color = oldColor;
		EditorGUILayout.EndHorizontal ();
	}
	
	PathMapBehaviour m_pathMapController;
	
	List<PathJointBehaviour> m_jointsToCleared;
	
	List<PathPairBehaviour> m_pathsToCleared;
}
